package com.cheneyjoo.impl;

import com.alibaba.fastjson.JSON;
import com.cheneyjoo.dao.UserDao;
import com.cheneyjoo.model.ResponseMessage;
import com.cheneyjoo.model.User;
import com.cheneyjoo.services.IUserService;
import com.cheneyjoo.util.Repository;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

@SuppressWarnings("ALL")
public class UserServiceImpl implements IUserService {
    private UserDao userDao;

    public UserServiceImpl() {
        try {
            userDao = new UserDao();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化用户实现操作类
     *
     * @param userDao
     */
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 登录接口
     *
     * @param type
     * @param account
     * @param pwd
     * @return
     */
    @Override
    public ResponseMessage login(int type, String account, String pwd) {
        if (1 == type && "admin".equals(account) && "123456".equals(pwd)) {
            return new ResponseMessage<User>(200, "success", new User(1, 1, "admin", null, "系统管理员"));
        } else if (2 == type) {
            User user = userDao.getUser(account);
            if (Optional.ofNullable(user).isPresent() && pwd.equals(user.getPassword())) {
                return new ResponseMessage<User>(200, "success", user);
            } else {
                return new ResponseMessage(201, "账号或密码输入错误", null);
            }
        } else {
            return new ResponseMessage(201, "账号或密码输入错误", null);
        }
    }

    /**
     * 注册接口
     *
     * @param user
     * @return
     */
    @Override
    public ResponseMessage register(String data) {
        User user = JSON.parseObject(data, User.class);
        boolean b = userDao.addUser(user);
        return b ? new ResponseMessage(200, "注册成功", null) : new ResponseMessage(201, "注册失败", null);
    }

    /**
     * 修改用户信息接口
     *
     * @param user
     * @return
     */
    @Override
    public ResponseMessage update(String data) {
        User user = JSON.parseObject(data, User.class);
        boolean b = userDao.updateUser(user);
        return b ? new ResponseMessage(200, "修改成功", null) : new ResponseMessage(201, "修改失败,用户名已存在", null);
    }

    /**
     * 删除用户接口
     *
     * @param id
     * @return
     */
    @Override
    public ResponseMessage delete(int id) {
        boolean b = userDao.deleteUser(id);
        return b ? new ResponseMessage(200, "删除成功", null) : new ResponseMessage(201, "删除失败", null);
    }

    /**
     * 获取全部用户接口
     *
     * @return
     */
    @Override
    public ResponseMessage get() {
        List<User> users = userDao.getUserList();
        return new ResponseMessage(200, "获取成功", users);
    }

    /**
     * 通过ID获取用户
     *
     * @param id
     * @return
     */
    @Override
    public ResponseMessage getById(String id) {
        User user = userDao.getUser(Integer.parseInt(id));
        if (Optional.ofNullable(user).isEmpty()) {
            return new ResponseMessage(201, "获取失败", user);
        }
        return new ResponseMessage(200, "获取成功", user);
    }

    /**
     * 通过用户名获取用户信息
     *
     * @param name
     * @return
     */
    @Override
    public ResponseMessage getByName(String name) {
        List<User> usersByAccount = userDao.getUsersByAccount(name);
        return new ResponseMessage(200, "获取成功", usersByAccount);
    }

    /**
     * 修改密码
     *
     * @param id
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @Override
    public ResponseMessage updatePwd(int id, String oldPwd, String newPwd) {
        boolean b = userDao.updatePwd(id, oldPwd, newPwd);
        return b ? new ResponseMessage(200, "修改成功", null) : new ResponseMessage(201, "修改失败,旧密码输入错误", null);
    }
}
